package com.corpgovernment.organization.mq;

import com.corpgovernment.common.mq.consumer.AbstractMultiTenantRocketMQConsumer;
import com.corpgovernment.common.mq.enums.MessageBizTypeEnum;
import com.corpgovernment.organization.entity.SyncOrganizationRequestEntity;
import com.corpgovernment.organization.service.ISyncOrganizationService;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@Slf4j
public class MultiTenantSyncOrganizationConsumer extends AbstractMultiTenantRocketMQConsumer {

    @Autowired
    private ISyncOrganizationService syncOrganizationService;

    @Override
    protected void consume(MessageExt messageExt, ConsumeConcurrentlyContext context) {
        try{
            String msg = new String(messageExt.getBody(), StandardCharsets.UTF_8);
            log.info("接收到的消息为：{}，MsgId：{}", msg, messageExt.getMsgId());
            if (StringUtils.isBlank(msg)){
                log.error("同步组织信息消费数据为空");
            }
            // 反序列化消息
            SyncOrganizationRequestEntity entity = JsonUtils.parse(msg, SyncOrganizationRequestEntity.class);
            syncOrganizationService.syncOrgInfo(entity, messageExt.getTags());
        } catch (Exception e){
            log.error("同步组织信息消费异常：{}", e);
        }
    }


    @Override
    protected MessageBizTypeEnum getMessageBizType() {
        return MessageBizTypeEnum.SYNC_ORGANIZATION_ADD;
    }
}
